#include "MatrixOperations.h"
#include <iostream>
#include <vector>

using namespace std;
using namespace Eigen;

void displayMenu() {
    cout << "\n=== 矩阵运算工具 ===" << endl;
    cout << "1. 创建矩阵" << endl;
    cout << "2. 矩阵加法" << endl;
    cout << "3. 矩阵减法" << endl;
    cout << "4. 矩阵与标量乘法" << endl;
    cout << "5. 矩阵与矩阵乘法" << endl;
    cout << "6. 矩阵转置" << endl;
    cout << "7. 显示矩阵属性" << endl;
    cout << "8. 显示所有矩阵" << endl;
    cout << "0. 退出" << endl;
    cout << "请选择操作: ";
}

void displayCreationMenu() {
    cout << "\n=== 矩阵创建选项 ===" << endl;
    cout << "1. 从控制台输入创建" << endl;
    cout << "2. 创建零矩阵" << endl;
    cout << "3. 创建单位矩阵" << endl;
    cout << "4. 创建全1矩阵" << endl;
    cout << "5. 创建随机矩阵" << endl;
    cout << "请选择创建方式: ";
}

int main() {
    vector<MatrixXd> matrices;
    int choice;
    
    cout << "欢迎使用基于Eigen库的矩阵运算工具!" << endl;
    
    do {
        displayMenu();
        cin >> choice;
        
        switch (choice) {
            case 1: { // 创建矩阵
                displayCreationMenu();
                int creationChoice;
                cin >> creationChoice;
                
                MatrixXd newMatrix;
                try {
                    switch (creationChoice) {
                        case 1:
                            newMatrix = MatrixOperations::createMatrixFromConsole();
                            break;
                        case 2: {
                            int rows = MatrixOperations::getIntegerInput("请输入行数: ");
                            int cols = MatrixOperations::getIntegerInput("请输入列数: ");
                            newMatrix = MatrixOperations::createZeroMatrix(rows, cols);
                            break;
                        }
                        case 3: {
                            int size = MatrixOperations::getIntegerInput("请输入矩阵大小: ");
                            newMatrix = MatrixOperations::createIdentityMatrix(size);
                            break;
                        }
                        case 4: {
                            int rows = MatrixOperations::getIntegerInput("请输入行数: ");
                            int cols = MatrixOperations::getIntegerInput("请输入列数: ");
                            newMatrix = MatrixOperations::createOnesMatrix(rows, cols);
                            break;
                        }
                        case 5: {
                            int rows = MatrixOperations::getIntegerInput("请输入行数: ");
                            int cols = MatrixOperations::getIntegerInput("请输入列数: ");
                            newMatrix = MatrixOperations::createRandomMatrix(rows, cols);
                            break;
                        }
                        default:
                            cout << "无效选择!" << endl;
                            continue;
                    }
                    
                    matrices.push_back(newMatrix);
                    cout << "矩阵创建成功! 已保存为矩阵 " << matrices.size() << endl;
                    MatrixOperations::printMatrix(newMatrix, "新创建的矩阵");
                    
                } catch (const exception& e) {
                    cout << "错误: " << e.what() << endl;
                }
                break;
            }
            
            case 2: { // 矩阵加法
                if (matrices.size() < 2) {
                    cout << "至少需要2个矩阵才能进行加法运算!" << endl;
                    break;
                }
                
                try {
                    int idx1 = MatrixOperations::getIntegerInput("选择第一个矩阵索引: ") - 1;
                    int idx2 = MatrixOperations::getIntegerInput("选择第二个矩阵索引: ") - 1;
                    
                    if (idx1 < 0 || idx1 >= matrices.size() || idx2 < 0 || idx2 >= matrices.size()) {
                        cout << "无效的矩阵索引!" << endl;
                        break;
                    }
                    
                    MatrixXd result = MatrixOperations::addMatrices(matrices[idx1], matrices[idx2]);
                    matrices.push_back(result);
                    
                    cout << "加法运算完成!" << endl;
                    MatrixOperations::printMatrix(matrices[idx1], "矩阵 " + to_string(idx1 + 1));
                    MatrixOperations::printMatrix(matrices[idx2], "矩阵 " + to_string(idx2 + 1));
                    MatrixOperations::printMatrix(result, "结果矩阵 " + to_string(matrices.size()));
                    
                } catch (const exception& e) {
                    cout << "错误: " << e.what() << endl;
                }
                break;
            }
            
            case 3: { // 矩阵减法
                if (matrices.size() < 2) {
                    cout << "至少需要2个矩阵才能进行减法运算!" << endl;
                    break;
                }
                
                try {
                    int idx1 = MatrixOperations::getIntegerInput("选择第一个矩阵索引: ") - 1;
                    int idx2 = MatrixOperations::getIntegerInput("选择第二个矩阵索引: ") - 1;
                    
                    if (idx1 < 0 || idx1 >= matrices.size() || idx2 < 0 || idx2 >= matrices.size()) {
                        cout << "无效的矩阵索引!" << endl;
                        break;
                    }
                    
                    MatrixXd result = MatrixOperations::subtractMatrices(matrices[idx1], matrices[idx2]);
                    matrices.push_back(result);
                    
                    cout << "减法运算完成!" << endl;
                    MatrixOperations::printMatrix(matrices[idx1], "矩阵 " + to_string(idx1 + 1));
                    MatrixOperations::printMatrix(matrices[idx2], "矩阵 " + to_string(idx2 + 1));
                    MatrixOperations::printMatrix(result, "结果矩阵 " + to_string(matrices.size()));
                    
                } catch (const exception& e) {
                    cout << "错误: " << e.what() << endl;
                }
                break;
            }
            
            case 4: { // 矩阵与标量乘法
                if (matrices.empty()) {
                    cout << "至少需要1个矩阵才能进行标量乘法运算!" << endl;
                    break;
                }
                
                try {
                    int idx = MatrixOperations::getIntegerInput("选择矩阵索引: ") - 1;
                    double scalar = MatrixOperations::getDoubleInput("输入标量值: ");
                    
                    if (idx < 0 || idx >= matrices.size()) {
                        cout << "无效的矩阵索引!" << endl;
                        break;
                    }
                    
                    MatrixXd result = MatrixOperations::multiplyMatrixScalar(matrices[idx], scalar);
                    matrices.push_back(result);
                    
                    cout << "标量乘法运算完成!" << endl;
                    MatrixOperations::printMatrix(matrices[idx], "原矩阵 " + to_string(idx + 1));
                    cout << "标量: " << scalar << endl;
                    MatrixOperations::printMatrix(result, "结果矩阵 " + to_string(matrices.size()));
                    
                } catch (const exception& e) {
                    cout << "错误: " << e.what() << endl;
                }
                break;
            }
            
            case 5: { // 矩阵与矩阵乘法
                if (matrices.size() < 2) {
                    cout << "至少需要2个矩阵才能进行矩阵乘法运算!" << endl;
                    break;
                }
                
                try {
                    int idx1 = MatrixOperations::getIntegerInput("选择第一个矩阵索引: ") - 1;
                    int idx2 = MatrixOperations::getIntegerInput("选择第二个矩阵索引: ") - 1;
                    
                    if (idx1 < 0 || idx1 >= matrices.size() || idx2 < 0 || idx2 >= matrices.size()) {
                        cout << "无效的矩阵索引!" << endl;
                        break;
                    }
                    
                    MatrixXd result = MatrixOperations::multiplyMatrices(matrices[idx1], matrices[idx2]);
                    matrices.push_back(result);
                    
                    cout << "矩阵乘法运算完成!" << endl;
                    MatrixOperations::printMatrix(matrices[idx1], "矩阵 " + to_string(idx1 + 1));
                    MatrixOperations::printMatrix(matrices[idx2], "矩阵 " + to_string(idx2 + 1));
                    MatrixOperations::printMatrix(result, "结果矩阵 " + to_string(matrices.size()));
                    
                } catch (const exception& e) {
                    cout << "错误: " << e.what() << endl;
                }
                break;
            }
            
            case 6: { // 矩阵转置
                if (matrices.empty()) {
                    cout << "至少需要1个矩阵才能进行转置运算!" << endl;
                    break;
                }
                
                try {
                    int idx = MatrixOperations::getIntegerInput("选择矩阵索引: ") - 1;
                    
                    if (idx < 0 || idx >= matrices.size()) {
                        cout << "无效的矩阵索引!" << endl;
                        break;
                    }
                    
                    MatrixXd result = MatrixOperations::transposeMatrix(matrices[idx]);
                    matrices.push_back(result);
                    
                    cout << "转置运算完成!" << endl;
                    MatrixOperations::printMatrix(matrices[idx], "原矩阵 " + to_string(idx + 1));
                    MatrixOperations::printMatrix(result, "转置矩阵 " + to_string(matrices.size()));
                    
                } catch (const exception& e) {
                    cout << "错误: " << e.what() << endl;
                }
                break;
            }
            
            case 7: { // 显示矩阵属性
                if (matrices.empty()) {
                    cout << "没有矩阵可显示!" << endl;
                    break;
                }
                
                int idx = MatrixOperations::getIntegerInput("选择矩阵索引: ") - 1;
                
                if (idx < 0 || idx >= matrices.size()) {
                    cout << "无效的矩阵索引!" << endl;
                    break;
                }
                
                MatrixOperations::printMatrixProperties(matrices[idx], "矩阵 " + to_string(idx + 1));
                break;
            }
            
            case 8: { // 显示所有矩阵
                if (matrices.empty()) {
                    cout << "没有矩阵可显示!" << endl;
                    break;
                }
                
                cout << "\n=== 所有矩阵 ===" << endl;
                for (size_t i = 0; i < matrices.size(); ++i) {
                    MatrixOperations::printMatrix(matrices[i], "矩阵 " + to_string(i + 1));
                }
                break;
            }
            
            case 0: // 退出
                cout << "感谢使用矩阵运算工具，再见!" << endl;
                break;
                
            default:
                cout << "无效选择，请重新输入!" << endl;
                break;
        }
        
        // 清除输入缓冲区
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        
    } while (choice != 0);
    
    return 0;
}